<template>
  <van-popup
    class="show-handwritten"
    v-model:show="showWriteSign"
    position="bottom"
    :style="{ height: '50%' }"
  >
    <div class="popup-content">
      <div class="popup-absolute">
        <div class="sign-top" @click="paintingSignatureClick">
          <svg-icon name="pen" className="pen-icon" />
          <span class="wirte-text">直接手绘签名</span>
        </div>
      </div>
      <template v-for="item in writeSignList" :kew="item">
        <div class="active-content">
          <div class="content" @click="selectWriteSignClick(item)">
            <img :src="item.sealImgData" alt="" />
            <span class="name-text">{{ item.sealName }}</span>
          </div>
        </div>
      </template>
    </div>
  </van-popup>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { useStore } from '@/store'
import { getSealSignatureList } from '@/api/contract'
import { Toast } from 'vant'
import { useRoute, useRouter } from 'vue-router'

export default defineComponent({
  emits: ['signBase64Img'],
  setup(props, { emit }) {
    const store = useStore()
    const router = useRouter()
    // 弹出显示
    const showWriteSign = ref(false)
    const currentSignType = ref('') // 签署类型
    const showWriteSignClick = (signType: string) => {
      currentSignType.value = signType
      showWriteSign.value = true
    }
    // 获取手绘签名
    const idNumber = ref('')
    const writeSignList = ref<any>([])
    idNumber.value = store.state.login.userInfo.idNumber
    const changeSealSignList = async () => {
      const res = await getSealSignatureList({
        idNumber: idNumber.value
      })
      if (res.code !== '0') return
      writeSignList.value = res.data.records
      if (writeSignList.value.length <= 0) {
        Toast.fail('手绘签名为空')
      }
    }
    changeSealSignList()

    // 选择手绘签名
    const selectWriteSignClick = (item: any) => {
      showWriteSign.value = false
      emit('signBase64Img', item.sealImgData, currentSignType.value)
    }

    // 手绘
    const paintingSignatureClick = () => {
      console.log(123)
      router.push('/painting-signature')
    }
    return {
      showWriteSign,
      writeSignList,
      showWriteSignClick,
      selectWriteSignClick,
      paintingSignatureClick
    }
  }
})
</script>

<style scoped lang="less">
.popup-content {
  position: relative;
  .popup-absolute {
    height: 50px;
    width: 100%;

    .sign-top {
      height: 50px;
      width: 100%;
      position: absolute;
      top: 0;
      left: 0;
      border-bottom: 1px solid #e0e0e0;
      display: flex;
      align-items: center;
      justify-content: center;
      .wirte-text {
        font-size: 14px;
        color: #2489f2;
      }
      .pen-icon {
        font-size: 18px;
        margin-right: 8px;
      }
    }
  }
  .content {
    height: 140px;
    width: calc(100% - 20px);
    display: flex;
    align-items: center;
    margin-left: 20px;
    border-bottom: 1px solid #e0e0e0;
    img {
      display: block;
      width: 100px;
      height: 84px;
      border: 3px solid #fe0000;
      border-radius: 4px;
      margin: 0 40px;
    }
    .name-text {
      font-size: 16px;
      font-weight: 700;
    }
  }
  .active-content:active {
    background-color: #f2f3f5;
  }
}
</style>
